#include <iostream>
using std::cout;
using std::endl;

class Singleton{
public:
    static Singleton * getInstance(){
        if(_pInstance == nullptr){
            _pInstance = new Singleton;
        }
        return _pInstance;
    }

    void init(int x,int y){
        _ix = x;
        _iy = y;
    }
    void print() const{
        cout << "(" << _ix << "," 
            << _iy << ")" << endl;
    }
    /* static void destroy(){ */
    /*     if(_pInstance){ */
    /*         delete _pInstance; */
    /*         _pInstance = nullptr; */
    /*     } */
    /* } */
    friend class AutoRelease;
private:
    Singleton() = default;
    ~Singleton(){ cout << "~Singleton()" << endl; }
private:
    int _ix;
    int _iy;
    static Singleton* _pInstance;
};
Singleton* Singleton::_pInstance = nullptr;


class AutoRelease{
public:
    AutoRelease(Singleton* p)
    : _p(p)
    { cout << "AutoRelease(Singleton*)" << endl; }

    ~AutoRelease(){
        cout << "~AutoRelease()" << endl;
        if(_p){
            delete _p;
            _p = nullptr;
        }
    }
private:
    Singleton* _p;
};

void test0(){
    /* Singleton * p = Singleton::getInstance(); */
    /* p->init(1,2); */
    /* p->print(); */
    AutoRelease ar(Singleton::getInstance());
    Singleton::getInstance()->init(100,200);
    Singleton::getInstance()->print();
}

int main(void){
    test0();
    return 0;
}
